package com.yaozheng.vocationaltraining.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.RelativeLayout;

import com.yaozheng.vocationaltraining.R;

/**
 * 主页学习进度加文字view
 */
public class HomeLearnTextProgressView extends View {
    /**
     * 外圆画笔对象
     */
    private Paint outerPaint;
    /**
     * 内圆进度画笔对象
     */
    private Paint insideProgressPaint;
    /**
     * 内圆没进度画笔对象
     */
    private Paint insidePaint;

    /**
     * 文字说明画笔对象
     */
    private Paint textExplanationPaint;

    /**
     * 当前进度
     */
    private int currProgress;

    int max = 100;

    /**
     * 边框大小
     */
    float outerPaintBorder, progressBorder;

    /**
     * 内圆边距
     */
    private float insideMargins;

    private String textExplanationStr;


    public HomeLearnTextProgressView(Context context) {
        super(context);
        initView();
    }

    public HomeLearnTextProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HomeLearnTextProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView() {
        outerPaintBorder = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, getResources()
                        .getDisplayMetrics());
        progressBorder = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                2, getResources().getDisplayMetrics());
        outerPaint = new Paint();
        outerPaint.setColor(Color.parseColor("#303030"));
//                getResources().getColor(
//                R.color.exam_menu_outer_border_color));
        outerPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        outerPaint.setStyle(Paint.Style.STROKE);// 设置空心
        outerPaint.setStrokeWidth(outerPaintBorder);

        insidePaint = new Paint();
        insidePaint.setColor(Color.parseColor("#b7b7b7"));
//                getResources().getColor(
//                R.color.progress_not_select_border_color));
        insidePaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insidePaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insidePaint.setStrokeWidth(progressBorder);
        insideProgressPaint = new Paint();
        insideProgressPaint.setColor(Color.parseColor("#b50032"));

//                getResources().getColor(
//                R.color.progress_select_border_color));
        insideProgressPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insideProgressPaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insideProgressPaint.setStrokeWidth(progressBorder);
        currProgress = 60;


        textExplanationPaint = new Paint();
        textExplanationPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        textExplanationPaint.setColor(Color.parseColor("#000000"));
        textExplanationPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.exam_content_font_size));
//        insideMargins = getWidth() / 2
//                + outerPaintBorder + progressBorder;

        textExplanationStr = "学习";

    }

    /**
     * 设置当前进度
     *
     * @param currProgress
     */
    public void setCurrProgress(int currProgress) {
        this.currProgress = currProgress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float width = getWidth() - outerPaintBorder;
        canvas.drawCircle(getWidth() / 2, getWidth() / 2, (width) / 2,
                outerPaint);

        insideMargins = getWidth() / 2
                + outerPaintBorder + progressBorder;

        float roundButtom = ((getWidth() - (insideMargins) / 2));
        /**
         * 弧形
         */
        RectF oval = new RectF(insideMargins / 2, insideMargins / 2, getWidth()
                - insideMargins / 2, roundButtom);
        int benginstartAngle = 76;
        /**
         * 红色进度弧形
         */
        canvas.drawArc(oval, 360 * benginstartAngle / max, 360 * currProgress
                / max, false, insideProgressPaint);

        /**
         * 灰色进度弧形
         */
        canvas.drawArc(oval, 360 * (currProgress + benginstartAngle) / max, 360
                * (100 - currProgress) / max, false, insidePaint);


//        canvas.drawText("学习",);

        Rect rect = new Rect();
        this.textExplanationPaint.getTextBounds(this.textExplanationStr, 0, this.textExplanationStr.length(), rect);
        float x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;;
        float y = (roundButtom + ((width - roundButtom) / 2) - rect.centerY());// 让显示的字体处于中心位置;;
        canvas.drawText(this.textExplanationStr, x, y, this.textExplanationPaint);


        super.onDraw(canvas);
    }


}
